using Cola.Core.Skill;
using Cola.Core.Skill.Utils;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.KernelExtensions;
using Microsoft.SemanticKernel.Orchestration;

namespace Cola.Core.Business.Ask;

public class AskClassifier : IAskClassifier {
    private IKernel _kernel;
    private ISKFunction _classify;
    private ILogger<AskClassifier> _logger;

    public AskClassifier(ILogger<IKernel> kernelLogger,
        IKernelProvider kernelProvider, ILogger<AskClassifier> logger) {
        _kernel = kernelProvider.GetKernel(kernelLogger);
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));

        _classify =
            _kernel.ImportSemanticSkillFromDirectory(
                ProjectFile.GetSkillsFolder(), "AskSemanticSkill")["Classify"];
    }

    public async Task<AskType> ClassifyAsync(string ask) {
        var context = new ContextVariables(ask);
        var result = await _kernel.RunAsync(context, _classify);
        return Enum.Parse<AskType>(result.Result);
    }
}